<?php
// +----------------------------------------------------------------------
// | 名片应用
// | Author: 张亚 < zhyacn@gmail.com>
// +----------------------------------------------------------------------
namespace app\businesscard\service;

use app\businesscard\model\BusinessCardModel;
use think\Db;

class CardService {

	public function getCardByUserId($userId = 0) {
		$filter['user_id'] = $userId;
		$isPaginate = 1;
		return $this->adminPostList($filter, $isPaginate);
	}

	public function adminCardList($filter) {
		return $this->adminPostList($filter);
	}

	/**
	 * [adminPostList description]
	 * @param  [type]  $filter     [description]
	 * @param  integer $isPaginate 0,按分页查找，1，查找一个，2，不分页查找
	 * @return [type]              [description]
	 */
	public function adminPostList($filter, $isPaginate = 0) {
		$where = [
			'a.create_time' => ['>=', 0],
			'a.delete_time' => 0,
		];

		$whereOr = [];

		$order = [
			'a.update_time' => 'DESC',
		];

		//前端用户登录查询个人的名片
		if (!empty($filter['user_id'])) {
			$where = [
				'a.user_id' => $filter['user_id'],
			];
		}

		$join = [
			['__USER__ u', 'a.user_id = u.id'],
		];

		$field = 'a.*,u.user_login,u.user_nickname,u.user_email,u.mobile';

		$category = empty($filter['category']) ? 0 : intval($filter['category']);
		if (!empty($category)) {
			$where['b.category_id'] = ['eq', $category];
			array_push($join, [
				'__BUSINESS_CATEGORY_CARD__ b', 'a.id = b.post_id',
			]);
			$field = 'a.*,b.id AS post_category_id,b.list_order,b.category_id,u.user_login,u.user_nickname,u.user_email,u.mobile';
		}

		$startTime = empty($filter['start_time']) ? 0 : strtotime($filter['start_time']);
		$endTime = empty($filter['end_time']) ? 0 : strtotime($filter['end_time']);
		if (!empty($startTime) && !empty($endTime)) {
			$where['a.published_time'] = [['>= time', $startTime], ['<= time', $endTime]];
		} else {
			if (!empty($startTime)) {
				$where['a.published_time'] = ['>= time', $startTime];
			}
			if (!empty($endTime)) {
				$where['a.published_time'] = ['<= time', $endTime];
			}
		}

		$keyword = empty($filter['keyword']) ? '' : $filter['keyword'];
		if (!empty($keyword)) {
			$where['a.name'] = ['like', "%$keyword%"];
			$whereOr['a.card_content'] = ['like', "%$keyword%"];

			$order = [
				'a.card_hits' => 'DESC',
			];
		}

		$user_id = empty($filter['user_id']) ? '' : $filter['user_id'];
		if (!empty($user_id)) {
			$where['a.user_id'] = $user_id;
		}

		$businessCardModel = new BusinessCardModel();

		if ($isPaginate == 1) {
			$cards = $businessCardModel::with('user')->alias('a')->field($field)
				->join($join)
				->where($where)
				->order($order)
				->find();

		} else if ($isPaginate == 2) {
			$limit = empty($filter['limit']) ? 10 : $filter['limit'];

			$cards = $businessCardModel::with('user')->alias('a')->field($field)
				->join($join)
				->where($where)
				->order($order)
				->limit($limit)
				->select();

		} else if ($isPaginate == 0) {
			$cards = $businessCardModel::with('user')->alias('a')->field($field)
				->join($join)
				->where($where)
				->order($order)
				->paginate(16);
		}

		return $cards;
	}

	/**
	 * [publishedCard description]
	 * @param  [type]  $postId     [description]
	 * @param  integer $categoryId [description]
	 * @return [type]              [description]
	 */
	public function publishedCard($postId) {
		$businessCardModel = new BusinessCardModel();
		$where = [
			'post.card_type' => 1,
			'post.card_status' => 1,
			'post.delete_time' => 0,
			'post.id' => $postId,
		];

		$join = [
			['__BUSINESS_CATEGORY_CARD__ relation', 'post.id = relation.post_id'],
		];
		$card = $businessCardModel->alias('post')->field('post.*')
			->join($join)
			->where($where)
			->find();

		return $card;
	}

	/**
	 * [refreshCardHits description]
	 * @param  [type] $cardId [description]
	 * @return [type]         [description]
	 */
	public function refreshCardHits($cardId) {
		Db::name('business_card')->where(['id' => $cardId])->setInc('card_hits');
	}

}